//
//  HomeFocusCell.m
//  OnionHome
//
//  Created by fengjie on 2019/9/16.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeFocusCell.h"

@interface HomeFocusCell ()
/**动态文字*/
@property (nonatomic,strong)UILabel*dynamicLabel;
/**动态图片*/
@property (nonatomic,strong)UIImageView*dynamicImage;
@end

@implementation HomeFocusCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView layoutIfNeeded];
        [self createUI];
    }
    return self;
}

-(void)setHomeFocusModel:(HomeFocusModel *)homeFocusModel{
    _homeFocusModel = homeFocusModel;
    
    self.topView.userName.text = homeFocusModel.name;
    self.topView.iconImage.image = IMAGE_NAMED(homeFocusModel.userIcon);
    self.dynamicImage.image = homeFocusModel.imageArray.lastObject;
    [TitleTool setDynamicTitle:homeFocusModel.title Label:self.dynamicLabel];
    [_bottomView.newsBtn setTitle:[NSString stringWithFormat:@"%ld",(long)homeFocusModel.commentCount] forState:UIControlStateNormal];
    
     [_bottomView.praiseBtn setTitle:[NSString stringWithFormat:@"%ld",(long)homeFocusModel.praiseCount] forState:UIControlStateNormal];
    
}
-(void)setSwitchModel:(HomeSwitchFocusModel *)switchModel{
    _switchModel = switchModel;
    self.topView.deleButton.hidden = YES;
    
    self.topView.userName.text = switchModel.userName;
    self.topView.iconImage.image = IMAGE_NAMED(switchModel.userImage);
    [TitleTool setDynamicTitle:switchModel.title Label:self.dynamicLabel];
    self.dynamicImage.image = IMAGE_NAMED(switchModel.imageArray.lastObject);
    [_bottomView.shareBtn setTitle:[NSString stringWithFormat:@"%ld",(long)switchModel.zhuanfaNum] forState:UIControlStateNormal];

    [_bottomView.newsBtn setTitle:[NSString stringWithFormat:@"%ld",(long)switchModel.xiaoxiNum] forState:UIControlStateNormal];
    
     [_bottomView.praiseBtn setTitle:[NSString stringWithFormat:@"%ld",(long)switchModel.zanNum] forState:UIControlStateNormal];

}
-(void)createUI{
    
    self.topView= [[UserIconAndNameView alloc] initWithFrame:CGRectZero];
    self.bottomView=[[ShareNewsPraiseView alloc] initWithFrame:CGRectZero];
    self.dynamicImage = UIImageView.new;
    self.dynamicImage.image = IMAGE_NAMED(@"dynamicDefut");
    
    [self addSubview:self.topView];
    [self addSubview:self.dynamicLabel];
    [self addSubview:self.bottomView];
    [self addSubview:self.dynamicImage];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [self.dynamicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.topView.mas_bottom);
    }];
    [self.dynamicImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(210);
        make.top.mas_equalTo(self.dynamicLabel.mas_bottom).offset(8);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.dynamicImage.mas_bottom);
    }];
}

-(UILabel*)dynamicLabel{
    if(!_dynamicLabel){
        _dynamicLabel=[UILabel new];
        _dynamicLabel.font = BOLDSYSTEMFONT(18);
        _dynamicLabel.textColor = UIColorFromRGB(0x273323);
        _dynamicLabel.numberOfLines = 2;
        _dynamicLabel.text = @"今天晚上做了法式薄煎饼，简单容易上手，老人孩子都喜欢吃，美味又健康，在家....";

    }
    return _dynamicLabel;
}



@end

@interface HomeFocusTwoImageCell ()

/**动态文字*/
@property (nonatomic,strong)UILabel*dynamicLabel;
/**左边动态图片*/
@property (nonatomic,strong)UIImageView*leftImage;
/**右边动态图片*/
@property (nonatomic,strong)UIImageView*rightImage;
@end

@implementation HomeFocusTwoImageCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self createUI];
    }
    return self;
}

-(void)setSwitchModel:(HomeSwitchFocusModel *)switchModel{
    _switchModel = switchModel;
    self.topView.deleButton.hidden = YES;
    
    self.topView.userName.text = switchModel.userName;
    self.topView.iconImage.image = IMAGE_NAMED(switchModel.userImage);
    [TitleTool setDynamicTitle:switchModel.title Label:self.dynamicLabel];
    self.leftImage.image = IMAGE_NAMED(switchModel.imageArray.firstObject);
    self.rightImage.image = IMAGE_NAMED(switchModel.imageArray.lastObject);

    [_bottomView.shareBtn setTitle:[NSString stringWithFormat:@"%ld",(long)switchModel.zhuanfaNum] forState:UIControlStateNormal];

    [_bottomView.newsBtn setTitle:[NSString stringWithFormat:@"%ld",(long)switchModel.xiaoxiNum] forState:UIControlStateNormal];
    
     [_bottomView.praiseBtn setTitle:[NSString stringWithFormat:@"%ld",(long)switchModel.zanNum] forState:UIControlStateNormal];

}
-(void)setHomeFocusModel:(HomeFocusModel *)homeFocusModel{
    _homeFocusModel = homeFocusModel;
    
    self.topView.userName.text = homeFocusModel.name;
    self.topView.iconImage.image = IMAGE_NAMED(homeFocusModel.userIcon);
    self.leftImage.image = homeFocusModel.imageArray.firstObject;
    self.rightImage.image = homeFocusModel.imageArray.lastObject;
    [TitleTool setDynamicTitle:homeFocusModel.title Label:self.dynamicLabel];

    [_bottomView.newsBtn setTitle:[NSString stringWithFormat:@"%ld",(long)homeFocusModel.commentCount] forState:UIControlStateNormal];
    
     [_bottomView.praiseBtn setTitle:[NSString stringWithFormat:@"%ld",(long)homeFocusModel.praiseCount] forState:UIControlStateNormal];

}
-(void)createUI{
    
    self.topView= [[UserIconAndNameView alloc] initWithFrame:CGRectZero];
    self.bottomView=[[ShareNewsPraiseView alloc] initWithFrame:CGRectZero];
    self.leftImage = UIImageView.new;
    self.leftImage.image = IMAGE_NAMED(@"dynamicDefut");
    
    self.rightImage = UIImageView.new;
    self.rightImage.image = IMAGE_NAMED(@"dynamicDefut");

    
    [self addSubview:self.topView];
    [self addSubview:self.dynamicLabel];
    [self addSubview:self.bottomView];
    [self addSubview:self.leftImage];
    [self addSubview:self.rightImage];

    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [self.dynamicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.topView.mas_bottom);
    }];
    CGFloat width = (KScreenWidth - 32 - 4)/2;
    [self.leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.width.height.mas_equalTo(width);
        make.top.mas_equalTo(self.dynamicLabel.mas_bottom).offset(16);
    }];
    [self.rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.width.height.mas_equalTo(width);
        make.top.mas_equalTo(self.leftImage);
    }];

    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.leftImage.mas_bottom);
    }];
}

-(UILabel*)dynamicLabel{
    if(!_dynamicLabel){
        _dynamicLabel=[UILabel new];
        _dynamicLabel.font = BOLDSYSTEMFONT(18);
        _dynamicLabel.textColor = UIColorFromRGB(0x273323);
        _dynamicLabel.numberOfLines = 2;
        _dynamicLabel.text = @"今天晚上做了法式薄煎饼，简单容易上手，老人孩子都喜欢吃，美味又健康，在家....";

    }
    return _dynamicLabel;
}




@end


@interface HomeFocusMorenImageCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

/**动态文字*/
@property (nonatomic,strong)UILabel*dynamicLabel;
/**底部小按钮视图*/
@property (nonatomic,strong)ShareNewsPraiseView *bottomView;
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;

@end

#define cell_width (KScreenWidth - 32- 4)/3


@implementation HomeFocusMorenImageCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self createUI];
    }
    return self;
}
-(void)setHomeCityModel:(HomeCityModel *)homeCityModel{
    _homeCityModel = homeCityModel;
//        self.dynamicLabel.text = homeCityModel.title;
    [TitleTool setDynamicTitle:homeCityModel.title Label:self.dynamicLabel];

    self.topView.iconImage.image = homeCityModel.userImage;
    self.dataArray = homeCityModel.imageArray;
    [self.listView reloadData];

    [self updateMasonry];
    
}
-(void)setIndusModel:(HomeIndustryDetailsModel *)indusModel{
    _indusModel = indusModel;
//        self.dynamicLabel.text = indusModel.title;
    [TitleTool setDynamicTitle:indusModel.title Label:self.dynamicLabel];

    self.topView.iconImage.image = indusModel.userImage;
    self.dataArray = indusModel.imageArray;
    [self.listView reloadData];

    [self updateMasonry];

}
-(void)setAnswermodel:(AnswerDetailModel *)answermodel{
    _answermodel = answermodel;
//        self.dynamicLabel.text = answermodel.title;
    [TitleTool setDynamicTitle:answermodel.title Label:self.dynamicLabel];

    self.topView.iconImage.image = IMAGE_NAMED(answermodel.userIcon);
    self.topView.userName.text = answermodel.name;
    self.dataArray = answermodel.imageArray;
    [self.listView reloadData];

    [self updateMasonry];

}
-(void)setHomeFocusModel:(HomeFocusModel *)homeFocusModel{
    _homeFocusModel = homeFocusModel;
    
//        self.dynamicLabel.text = homeFocusModel.title;
    [TitleTool setDynamicTitle:homeFocusModel.title Label:self.dynamicLabel];
    self.topView.iconImage.image = IMAGE_NAMED(homeFocusModel.userIcon);
    self.topView.userName.text = homeFocusModel.name;
    self.dataArray = homeFocusModel.imageArray;
    [self.listView reloadData];
    
    [self updateMasonry];

}
-(void)updateMasonry{
    NSInteger count = 0;
    if (self.dataArray.count == 3) {
        count = 1;
    }
    if (self.dataArray.count > 3 && self.dataArray.count <=6) {
        count = 2;
    }else if (self.dataArray.count > 6 && self.dataArray.count <= 9){
        count = 3;
    }
    
    [self.listView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(count * cell_width);
    }];
    [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.listView.mas_bottom);
    }];
}
-(void)createUI{
    
    self.topView= [[UserIconAndNameView alloc] initWithFrame:CGRectZero];
    self.bottomView=[[ShareNewsPraiseView alloc] initWithFrame:CGRectZero];

    [self addSubview:self.topView];
    [self addSubview:self.dynamicLabel];
    [self addSubview:self.bottomView];
    [self addSubview:self.listView];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [self.dynamicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.topView.mas_bottom);
    }];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(13);
        make.right.mas_equalTo(-13);
//        make.height.mas_equalTo(210);
        make.top.mas_equalTo(self.dynamicLabel.mas_bottom).offset(8);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.listView.mas_bottom);
    }];
}

-(UILabel*)dynamicLabel{
    if(!_dynamicLabel){
        _dynamicLabel=[UILabel new];
        _dynamicLabel.font = BOLDSYSTEMFONT(18);
        _dynamicLabel.textColor = UIColorFromRGB(0x273323);
        _dynamicLabel.numberOfLines = 2;
        _dynamicLabel.text = @"今天晚上做了法式薄煎饼，简单容易上手，老人孩子都喜欢吃，美味又健康，在家....";
    }
    return _dynamicLabel;
}

#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count ;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImagesCollectionItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(ImagesCollectionItem.class) forIndexPath:indexPath];
    if ([self.dataArray[indexPath.row] isKindOfClass:[NSString class]]) {
        cell.imageView.image = IMAGE_NAMED(self.dataArray[indexPath.row]);
    }else{
        cell.imageView.image = self.dataArray[indexPath.row];
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(cell_width, cell_width);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 2;
}
// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        _listView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = NO;
        [_listView registerClass:[ImagesCollectionItem class] forCellWithReuseIdentifier:NSStringFromClass(ImagesCollectionItem.class)];
    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc] init];
    }
    return _dataArray;
}
@end

@interface ImagesCollectionItem ()
@end

@implementation ImagesCollectionItem

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}
-(void)createUI{
    self.imageView = UIImageView.new;
    [self addSubview:self.imageView];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
@end
